package com.team.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.Exception.ServiceException;
import com.team.commons.dto.JourneyDto;
import com.team.commons.utils.ColaBeanUtils;
import com.team.commons.vo.JourneyVo;
import com.team.entity.Journey;
import com.team.mapper.JourneyMapper;
import com.team.result.StatusCode;
import com.team.service.JourneyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class JourneyServiceImpl implements JourneyService {

    @Resource
    JourneyMapper journeyMapper;


    @Override
    public JourneyVo selectJourneyById(Long productId) {
        JourneyVo vo=new JourneyVo();
        try {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq(Journey.COL_PRODUCT_ID,productId);
            Journey journey = journeyMapper.selectOne(queryWrapper);
            ColaBeanUtils.copyProperties(journey,vo);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return vo;
    }

    @Override
    public Integer insertJourney(JourneyDto journeyDto) {
        int insert=0;
        try {
            Journey journey=new Journey();
            ColaBeanUtils.copyProperties(journeyDto,journey);
            insert = journeyMapper.insert(journey);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        if(insert==0){
            throw new ServiceException(StatusCode.INTER_ERROR);
        }
        return insert;
    }

    @Override
    public Integer updateJourney(JourneyDto journeyDto) {
        int insert=0;
        try {
            Journey journey=new Journey();
            ColaBeanUtils.copyProperties(journeyDto,journey);
            insert = journeyMapper.updateById(journey);
        }catch (Exception e){
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        if(insert==0){
            throw new ServiceException(StatusCode.UPDATE_ERROR);
        }
        return insert;
    }
}
